package com.syni.mdd.yhd.common.all.utils;

import com.github.qcloudsms.SmsSingleSender;
import com.github.qcloudsms.SmsSingleSenderResult;
import com.github.qcloudsms.httpclient.HTTPException;
import com.syni.mdd.sjb.common.component.utils.common.utils.AppConfig;
import lombok.extern.log4j.Log4j2;
import org.json.JSONException;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

/**
 * @className SmsUtils
 * @description TOO
 * @Author cfx
 * @DATE 2019/6/22 8:48
 * @VERSION 1.0
 **/
@Log4j2
public class SmsUtils {
    private final static String CHINA_CODE = "86";

    public static String getPorperty(String porperty){
        return AppConfig.getProperty("cloud.sms." + porperty);
    }
    public static String getPorperty(String sign,String porperty){
        return AppConfig.getProperty(String.format("cloud.sms.%s.%s",sign,porperty));
    }


    /*public static void send(String phone,String identifyCode){
        try {
            String appId = getPorperty("appId");
            String appKey = getPorperty("appKey");
            Integer templateId = Integer.parseInt(getPorperty("templateId"));
            String sign = new String(getPorperty("sign").getBytes("iso-8859-1"),"utf-8");
            String[] params = {identifyCode};
            SmsSingleSender ssender = new SmsSingleSender(Integer.parseInt(appId),appKey);
            SmsSingleSenderResult result = ssender.sendWithParam(CHINA_CODE, phone,
                    templateId, params, sign, "", "");  // 签名参数未提供或者为空时，会使用默认签名发送短信
            System.out.println(result);
        } catch (HTTPException e) {
            // HTTP 响应码错误
            log.error(e.getMessage(),e);
        } catch (JSONException e) {
            // JSON 解析错误
            log.error(e.getMessage(),e);
        } catch (IOException e) {
            // 网络 IO 错误
            log.error(e.getMessage(),e);
        }
    }*/


    public static void send(String phone,String templateIds,String[] params) {
        try {
            String sign = new String(getPorperty("sign").getBytes("iso-8859-1"),"utf-8");
            handSend(phone,templateIds,params,sign);
        } catch (UnsupportedEncodingException e) {
            log.error(e.getMessage(),e);
        }
    }

    public static void customSend(String phone,String templateIds,String[] params) {
        try {
            String sign = new String(getPorperty("business.sign").getBytes("iso-8859-1"),"utf-8");
            boolean result = false;
            try {
                String appId = getPorperty("appId");
                String appKey = getPorperty("appKey");
                Integer templateId = Integer.parseInt(templateIds);
                //String sign = new String(getPorperty("sign").getBytes("iso-8859-1"),"utf-8");
                SmsSingleSender ssender = new SmsSingleSender(Integer.parseInt(appId),appKey);
                SmsSingleSenderResult senderResult = ssender.sendWithParam(CHINA_CODE, phone,
                        templateId, params, sign, "", "");  // 签名参数未提供或者为空时，会使用默认签名发送短信
                //log.info(JSON.toJSONString(senderResult));
                result = senderResult.result == 1 ? true : false;
                log.info("sendResult:{}",result);
            } catch (Exception e) {
                // HTTP 响应码错误
                log.error(e.getMessage(),e);
            }
//            catch (JSONException e) {
//                // JSON 解析错误
//                log.error(e.getMessage(),e);
//            } catch (IOException e) {
//                // 网络 IO 错误
//                log.error(e.getMessage(),e);
//            }
        } catch (UnsupportedEncodingException e) {
            log.error(e.getMessage(),e);
        }
    }

    public static boolean send(String phone,String templateIds,String[] params,String client) {
        boolean sendResult = false;
        try {
            String sign = new String(getPorperty(client,"sign").getBytes("iso-8859-1"),"utf-8");
            sendResult = handSend(phone,templateIds,params,sign);
        } catch (UnsupportedEncodingException e) {
            log.error(e.getMessage(),e);
        }
        return sendResult;
    }

    public static boolean thirdPartySend(String phone,String templateIds,String[] params,String client){
        boolean sendResult = false;
        try {
            String sign = new String(getPorperty(client,"sign").getBytes("iso-8859-1"),"utf-8");
            String appId = getPorperty("appId");
            String appKey = getPorperty("appKey");
            Integer templateId = Integer.parseInt(templateIds);
            SmsSingleSender ssender = new SmsSingleSender(Integer.parseInt(appId),appKey);
            SmsSingleSenderResult senderResult = ssender.sendWithParam(CHINA_CODE, phone,
                    templateId, params, sign, "", "");  // 签名参数未提供或者为空时，会使用默认签名发送短信
            sendResult = senderResult.result == 0 ? true : false;
        } catch (UnsupportedEncodingException e) {
            log.error(e.getMessage(),e);
        } catch (IOException e) {
            log.error(e.getMessage(),e);
        } catch (HTTPException e) {
            log.error(e.getMessage(),e);
        }
        return sendResult;
    }

    public static boolean handSend(String phone,String templateIds,String[] params,String sign){
        boolean result = false;
        try {
            String appId = getPorperty("appId");
            String appKey = getPorperty("appKey");
            Integer templateId = Integer.parseInt(getPorperty(templateIds));
            //String sign = new String(getPorperty("sign").getBytes("iso-8859-1"),"utf-8");
            SmsSingleSender ssender = new SmsSingleSender(Integer.parseInt(appId),appKey);
            SmsSingleSenderResult senderResult = ssender.sendWithParam(CHINA_CODE, phone,
                    templateId, params, sign, "", "");  // 签名参数未提供或者为空时，会使用默认签名发送短信
            result = senderResult.result == 1 ? true : false;
        } catch (HTTPException e) {
            // HTTP 响应码错误
            log.error(e.getMessage(),e);
        } catch (JSONException e) {
            // JSON 解析错误
            log.error(e.getMessage(),e);
        } catch (IOException e) {
            // 网络 IO 错误
            log.error(e.getMessage(),e);
        }
        return result;
    }
}
